package com.zl.controller;

import com.zl.common.Result;
import com.zl.dto.TsRoleDto;
import com.zl.dto.base.TsPageInfoDto;
import com.zl.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping(value = "/roles/add")
    // @PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_CREATE')")
    public Result create(@RequestBody TsRoleDto resources){
        return roleService.create(resources);
    }

    @PutMapping(value = "/roles/update")
    //@PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_EDIT')")
    public Result update(@RequestBody TsRoleDto resources){
        return roleService.update(resources);
    }

    /*@DeleteMapping(value = "delete")
    //@PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_DELETE')")
    public Result delete(@PathVariable Long id){
        return roleService.delete(id);
    }*/

    /**
     * 获取单个role
     * @param id
     * @return
     */
//    @GetMapping(value = "/roles/{id}")
//    public ResponseEntity getRoles(@PathVariable Long id){
//        return new ResponseEntity(roleService.findById(id), HttpStatus.OK);
//    }

    /**
     * 返回全部的角色，新增用户时下拉选择
     * @return
     */
    @GetMapping(value = "/roles/all")
    public ResponseEntity getAll(TsPageInfoDto pageInfoDto){
//        PageInfo<TsRoleDto> roleList= roleService.queryAll(pageInfoDto);
        return new ResponseEntity(roleService.queryAll(pageInfoDto), HttpStatus.OK);
    }
//
//    @GetMapping(value = "/roles")
//    public ResponseEntity getRoles(RoleQueryCriteria criteria, Pageable pageable){
//        return new ResponseEntity(roleService.queryAll(criteria,pageable), HttpStatus.OK);
//    }
//
//    @GetMapping(value = "/roles/level")
//    public ResponseEntity getLevel(){
//        List<Integer> levels = roleService.findByUsers_Id(SecurityUtils.getUserId()).stream().map(RoleSmallDTO::getLevel).collect(Collectors.toList());
//        return new ResponseEntity(Dict.create().set("level", Collections.min(levels)), HttpStatus.OK);
//    }
//
//    @PostMapping(value = "/roles")
//    public ResponseEntity create(@Validated @RequestBody Role resources){
//        if (resources.getId() != null) {
//            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
//        }
//        return new ResponseEntity(roleService.create(resources), HttpStatus.CREATED);
//    }
//
//    @PutMapping(value = "/roles")
//    public ResponseEntity update(@Validated(Role.Update.class) @RequestBody Role resources){
//        roleService.update(resources);
//        return new ResponseEntity(HttpStatus.NO_CONTENT);
//    }
//
//    @PutMapping(value = "/roles/permission")
//    public ResponseEntity updatePermission(@RequestBody Role resources){
//        roleService.updatePermission(resources,roleService.findById(resources.getId()));
//        return new ResponseEntity(HttpStatus.NO_CONTENT);
//    }
//
//    @PutMapping(value = "/roles/menu")
//    public ResponseEntity updateMenu(@RequestBody Role resources){
//        roleService.updateMenu(resources,roleService.findById(resources.getId()));
//        return new ResponseEntity(HttpStatus.NO_CONTENT);
//    }

    @DeleteMapping(value = "/roles/{id}")
    public ResponseEntity delete(@PathVariable Long id){
        roleService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
